﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class ChartScoreAnswerAnalyze : System.Web.UI.Page
    {
        protected int examroomid = 0, studentid = 0;
        protected bool isquery = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                if (!UserValidate.CheckQuery())
                {
                    Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                    Response.Redirect("login.aspx");
                    Response.End();
                }
                else
                {
                    isquery = true;
                }
            }
            if (isquery)
            {
                studentid = int.Parse(HttpContext.Current.Session["QueryStudentId"] as string);
            }
            else if (!PageValidate.QueryStringIsEmpty("studentid"))
            {
                try
                {
                    studentid = int.Parse(PageValidate.QueryString("studentid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (examroomid > 0 && studentid > 0)
                {
                    IList<StudentScoreExtendInfo> infos = new List<StudentScoreExtendInfo>();
                    infos = StudentScoreExtend.GetList(examroomid, 0, 0, 0, 0, 0, 0, 0);
                    StudentScoreExtendInfo myInfo = infos.Single(t => t.StudentId == studentid);
                    ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examroomid);
                    if (myInfo.CountScore == myInfo.ExamPaperScore)
                    {
                        AnswerAnalyzeList.InnerHtml = "<tr><td colspan='6'>恭喜你得满分，所有答案全部正确</td></tr>";
                    }
                    else
                    {
                        AnswerAnalyzeList.InnerHtml = AnswerAnalyze(myInfo, examroomInfo.ExamPaperId);
                    }
                }
            }
        }

        private string AnswerAnalyze(StudentScoreExtendInfo myInfo,int exampaperid)
        {
            StringBuilder sb = new StringBuilder();
            IList<ScoreInfo> scoreInfos = ScoreHelper.GetScoreList(myInfo.QuestItemScore);
            IList<PaperQuestionInfo> infos = new List<PaperQuestionInfo>();
            ExamPaperInfo examPaperInfo = BAL.ExamPaper.GetT1(exampaperid);
            infos = Question.GetList(examPaperInfo.Id);
            if (examPaperInfo.TestPaperPolicyMainId == null)
            {
                examPaperInfo.TestPaperPolicyMainId = 0;
            }
            IList<TestPaperPolicySubInfo> tpinfo = TestPaperPolicySub.GetList((int)examPaperInfo.TestPaperPolicyMainId);
            int a = 1;
            foreach (TestPaperPolicySubInfo info in tpinfo)
            {
                IList<PaperQuestionInfo> tInfos = infos.Where(t => t.SubPolicyId == info.Id).ToList();
                foreach (PaperQuestionInfo qinfo in tInfos)
                {
                    if (scoreInfos.Count > 0)
                    {
                        ScoreInfo scoreInfo = scoreInfos.Single(t => t.Id == qinfo.Id);
                        if (decimal.Parse(scoreInfo.Score) != qinfo.Score)
                        {
                            string knowname = Knowledge.GetKnowledgeInfoById(qinfo.KnowId).Name;
                            sb.Append("<tr>");
                            sb.Append("<td>");
                            sb.Append(a.ToString());
                            sb.Append("</td>");
                            sb.Append("<td class='text_left'>");
                            sb.Append(knowname);
                            sb.Append("</td>");
                            sb.Append("<td>");
                            sb.Append(scoreInfo.Score);
                            sb.Append("</td>");
                            sb.Append("<td>");
                            sb.Append(qinfo.Score.ToString());
                            sb.Append("</td>");
                            sb.Append("<td class='text_left'>");
                            sb.Append(scoreInfo.Answer);
                            sb.Append("</td>");
                            sb.Append("<td class='text_left'>");
                            sb.Append(qinfo.Answer);
                            sb.Append("</td>");
                            sb.Append("</tr>");
                        }
                        a++;
                    }
                }
            }
            return sb.ToString();
        }
    }
}
